package com.htgd.gardenexpo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.htgd.gardenexpo.entity.CommonResult;
import com.htgd.gardenexpo.entity.GeConCompetitionPrize;
import com.htgd.gardenexpo.dao.GeConCompetitionPrizeMapper;
import com.htgd.gardenexpo.entity.GeConLeisurePrize;
import com.htgd.gardenexpo.page.PrizeQuery;
import com.htgd.gardenexpo.service.GeBaseDictService;
import com.htgd.gardenexpo.service.IGeConCompetitionPrizeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htgd.gardenexpo.vo.GeConCompetitionPrizeVO;
import com.htgd.gardenexpo.vo.GeConCompetitionSportsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 赛事奖品 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-26
 */
@Service
public class GeConCompetitionPrizeServiceImpl extends ServiceImpl<GeConCompetitionPrizeMapper, GeConCompetitionPrize> implements IGeConCompetitionPrizeService {

    @Autowired
    private GeConCompetitionPrizeMapper geConCompetitionPrizeMapper;

    @Autowired
    private GeBaseDictService geBaseDictService;

    @Override
    public PageInfo<GeConCompetitionPrizeVO> pageList(PrizeQuery prizeQuery) {
        PageHelper.startPage(prizeQuery.getPageNum(), prizeQuery.getPageSize());
        List<GeConCompetitionPrizeVO> list= geConCompetitionPrizeMapper.pageList(prizeQuery);
        for (GeConCompetitionPrizeVO geConCompetitionPrizeVO : list) {
            if(geConCompetitionPrizeVO.getType()!=null){
                geConCompetitionPrizeVO.setTypeText(geBaseDictService.getTextByCodeAndValue("type_prize_competiton",geConCompetitionPrizeVO.getType()));
            }
            if(geConCompetitionPrizeVO.getGrade()!=null){
                geConCompetitionPrizeVO.setGradeText(geBaseDictService.getTextByCodeAndValue("grade_prize_competiton",geConCompetitionPrizeVO.getGrade()));
            }
        }
        return new PageInfo<>(list);
    }

    @Override
    public CommonResult add(GeConCompetitionPrize geConCompetitionPrize) {
        //检查这一类是否重复添加奖项
        boolean exists = lambdaQuery().eq(GeConCompetitionPrize::getSportId, geConCompetitionPrize.getSportId())
                .eq(GeConCompetitionPrize::getGrade, geConCompetitionPrize.getGrade()).exists();
        if(exists){
            return CommonResult.error("该赛事下不能重复添加相同的奖项");
        }else {
            geConCompetitionPrizeMapper.insert(geConCompetitionPrize);
        }
        return CommonResult.success("添加成功");
    }

    @Override
    public CommonResult edit(GeConCompetitionPrize geConCompetitionPrize) {
        lambdaUpdate().eq(GeConCompetitionPrize::getId,geConCompetitionPrize.getId())
                .set(GeConCompetitionPrize::getName,geConCompetitionPrize.getName())
                .set(GeConCompetitionPrize::getPicture,geConCompetitionPrize.getPicture())
                .set(GeConCompetitionPrize::getType,geConCompetitionPrize.getType())
                .set(GeConCompetitionPrize::getNum,geConCompetitionPrize.getNum())
                .set(GeConCompetitionPrize::getGrade,geConCompetitionPrize.getGrade())
                .update();
        return CommonResult.success("编辑成功");
    }
}
